package com.zjsd.sqeybusiness.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.zjsd.sqeybusiness.domain.*;
import com.zjsd.sqeybusiness.service.IShareInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 圈子发布信息Controller
 * @author ren
 */
@RestController
@RequestMapping("/jph/ShareInfo")
public class ShareInfoController extends BaseController {

    @Autowired
    private IShareInfoService shareInfoService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询圈子信息列表
     */
    @PreAuthorize("@ss.hasPermi('jph:ShareInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(ShareInfo shareInfo)
    {
        startPage();
        List<ShareInfo> list = shareInfoService.selectShareInfoList(shareInfo);
        return getDataTable(list);
    }

    /**
     * 导出圈子信息列表
     */
    @PreAuthorize("@ss.hasPermi('jph:ShareInfo:export')")
    //@Log(title = "圈子信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ShareInfo shareInfo)
    {
        List<ShareInfo> list = shareInfoService.selectShareInfoList(shareInfo);
        ExcelUtil<ShareInfo> util = new ExcelUtil<ShareInfo>(ShareInfo.class);
        return util.exportExcel(list, "ShareInfo");
    }

    /**
     * 获取圈子信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('jph:ShareInfo:query')")
    @GetMapping(value = "/{shareid}")
    public AjaxResult getInfo(@PathVariable("shareid") Long shareid)
    {
        return AjaxResult.success(shareInfoService.selectShareInfoById(shareid));
    }

    /**
     * 新增圈子信息
     */
    @PreAuthorize("@ss.hasPermi('jph:ShareInfo:add')")
    //@Log(title = "圈子信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ShareInfo shareInfo)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        SysDept sysDept = shareInfoService.getDeptInfoByUserId(user.getUserId());
        CompanyInfo companyInfo = shareInfoService.getCompanyInfoByDeptId(sysDept.getDeptId());
        if(companyInfo != null){
            shareInfo.setCompanyid(companyInfo.getCompanyId());
        }
        return toAjax(shareInfoService.insertShareInfo(shareInfo));
    }

    /**
     * 修改圈子信息
     */
    @PreAuthorize("@ss.hasPermi('jph:ShareInfo:edit')")
    //@Log(title = "圈子信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ShareInfo shareInfo)
    {
        return toAjax(shareInfoService.updateShareInfo(shareInfo));
    }

    /**
     * 删除圈子信息
     */
    @PreAuthorize("@ss.hasPermi('jph:ShareInfo:remove')")
    //@Log(title = "圈子信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{shareids}")
    public AjaxResult remove(@PathVariable Long[] shareids)
    {
        return toAjax(shareInfoService.deleteShareInfoByIds(shareids));
    }

}
